import { Module } from 'vuex'
import { RootState } from '@/store/types'
import { getDataList as fetchIcafeCategoryList } from '@/api/customer/category'
import { getDataList as fetchIspList } from '@/api/customer/isp'

export interface IcafeModuleState {
  category: any[]
  isp: any[]
}

const icafeModule: Module<IcafeModuleState, RootState> = {
  namespaced: true,
  state: () => {
    return {
      category: [],
      isp: []
    }
  },
  getters: {
    categoryOptions(store) {
      return store.category.map((item) => {
        return { label: item.name, value: item.id, disabled: item.status === 0 }
      })
    },
    ispOptions(store) {
      return store.isp.map((item) => {
        return { label: item.name, value: item.id, disabled: item.status === 0 }
      })
    }
  },
  mutations: {
    changeCategory(state, payload) {
      state.category = payload
    },
    changeIsp(state, payload) {
      state.isp = payload
    }
  },
  actions: {
    async init({ commit }) {
      // 请求完整分类信息
      const category = (await fetchIcafeCategoryList())?.data?.list ?? []
      commit('changeCategory', category)

      // 请求完整运营商信息
      const isp = (await fetchIspList())?.data?.list ?? []
      commit('changeIsp', isp)
    }
  }
}

export default icafeModule
